/*
 * Copyright (c) 2023, 2024,yzc.cn All rights reserved.
 *
 */
package com.by.server.impl;

import com.by.common.utils.AutoCode;
import com.by.common.utils.AutoData;
import com.by.mapper.PurContractDetailsMapper;
import com.by.mapper.PurContractMapper;
import com.by.pojo.PurContract;
import com.by.server.PurContractServer;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>Project: erp-cloud-api - PurContractServerImpl</p>
 * <p>Powered by yzc On 2024-01-15 14:25:27</p>
 * <p>描述：<p>
 *
 * @author yzc [yzc0415@126.com]
 * @version 1.0
 * @since 17
 */
@Service
public class PurContractServerImpl implements PurContractServer {

    @Autowired
    PurContractMapper purContractMapper;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    PurContractDetailsMapper purContractDetailsMapper;

    @Override
    public int save(PurContract purContract) {
        //获得前缀
        String code = AutoCode.autoCodePrefix("合同")+ AutoData.getNowData();
        //查询最大code
        String maxCode = purContractMapper.maxCode(code);
        //获得新的code
        String newCode = AutoCode.getNewCode(maxCode,code);
        //设置code
        purContract.setCode(newCode);
        //遍历合同产品详情表
        purContract.getPurContractDetailsList().forEach(purContractDetails -> {
            //设置关联字段
            purContractDetails.setContractCode(newCode);
            //保存产品详情表
            purContractDetailsMapper.insert(purContractDetails);
        });

        HashMap<String,Object> map = new HashMap<>();
        map.put("name","合同付款计划");
        map.put("batch",2);
        map.put("contractCode",newCode);
        rabbitTemplate.convertAndSend("finPlanExchange","finPlan",map);




        return purContractMapper.insert(purContract);
    }

    @Override
    public int updateStatus(String code, int status) {
        int rs = purContractMapper.updateStatus(code, status);
        return rs;
    }
}
